package com.dw.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dw.entity.Test;
import com.dw.entity.User;
import com.dw.service.TestService;
import com.dw.service.UserService;
import com.google.gson.Gson;

@Controller
@RequestMapping(value="/test")
public class TestController {
	@Autowired
	TestService testService;
	
	@Autowired
	UserService userService;
	
	@RequestMapping(value="/add")
	public String add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		int id = 1;
		
		User user = userService.findById(id);
		Test test = new Test();
		test.setRemark("添加备注");
		test.setUser(user);
		
		testService.addTest(test);
		out.print("添加用户");
		return null;
	}
	
	@RequestMapping(value="/list")
	public String list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		List<Test> test = testService.list();
		
		Gson g = new Gson();
		out.print(g.toJson(test));
		
		return null;
	}
}
